import { login, getInfo, updateAttr } from '@/api/user'
import { getToken, setToken, removeToken } from '@/util/auth'

const getDefaultState = () => {
  return {
    token: getToken(),
    user: uni.getStorageSync('user'),
		name: uni.getStorageSync('name'),
		phone: uni.getStorageSync('phone'),
    avatar: uni.getStorageSync('avatar'),
		superable: uni.getStorageSync('superable'),
		connectSend: uni.getStorageSync('connectSend'),
		connectSendStr: uni.getStorageSync('connectSendStr'),
		polling: uni.getStorageSync('polling'),
		pollingInter: uni.getStorageSync('pollingInter'),
		deviceNameAttr: uni.getStorageSync('deviceNameAttr'),
		connectTimeout: uni.getStorageSync('connectTimeout'),
		mqttHost: uni.getStorageSync('mqttHost') || 'www.zesi.com.cn',
		mqttPort: uni.getStorageSync('mqttPort') || 9082,
		mqttUser: uni.getStorageSync('mqttUser') || 'user',
		mqttPassword: uni.getStorageSync('mqttPassword') || 'Dev1@3$5',
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
	SET_USER: (state, user) => {
	  state.user = user
		uni.setStorageSync('user',user)
	},
  SET_NAME: (state, name) => {
    state.name = name
		uni.setStorageSync('name',name)
  },
	SET_PHONE: (state, phone) => {
	  state.phone = phone
		uni.setStorageSync('phone',phone)
	},
	SET_SUPERABLE: (state, superable) => {
	  state.superable = superable
		uni.setStorageSync('superable',superable)
	},
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
		uni.setStorageSync('avatar',avatar)
  },
  SET_CONNECTSEND: (state, data) => {
    state.connectSend = data
		uni.setStorageSync('connectSend',data)
  },
  SET_CONNECTSENDSTR: (state, data) => {
    state.connectSendStr = data
		uni.setStorageSync('connectSendStr',data)
  },
  SET_POLLING: (state, data) => {
    state.polling = data
		uni.setStorageSync('polling',data)
  },
  SET_POLLINGINTER: (state, data) => {
    state.pollingInter = data
		uni.setStorageSync('pollingInter',data)
  },
  SET_DEVICENAMEATTR: (state, data) => {
    state.deviceNameAttr = data
		uni.setStorageSync('deviceNameAttr',data)
  },
  SET_CONNECTTIMEOUT: (state, data) => {
    state.connectTimeout = data
		uni.setStorageSync('connectTimeout',data)
  },
  SET_MQTTHOST: (state, data) => {
    state.mqttHost = data
		uni.setStorageSync('mqttHost',data)
  },
  SET_MQTTPORT: (state, data) => {
    state.mqttPort = data
		uni.setStorageSync('mqttPort',data)
  },
  SET_MQTTUSER: (state, data) => {
    state.mqttUser = data
		uni.setStorageSync('mqttUser',data)
  },
  SET_MQTTPASSWORD: (state, data) => {
    state.mqttPassword = data
		uni.setStorageSync('mqttPassword',data)
  }
}

const actions = {
	updateAttr({ commit }, info) {
	  return new Promise((resolve, reject) => {
	    updateAttr(info).then(response => {
	      const { data } = response
	      commit('SET_NAME', info.name)
	      resolve(data)
	    }).catch(error => {
	      reject(error)
	    })
	  })
	},
	
  // user login
  login({ commit }, userInfo) {
    const { phone, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ phone: phone.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, phone, avatar, superable } = data
				commit('SET_USER', data)
        commit('SET_NAME', name)
				commit('SET_PHONE', phone)
        commit('SET_AVATAR', avatar)
				commit('SET_SUPERABLE', superable)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      try {
        const keys = ['intelirri-devices', 'intelirri-devicesObj']
        keys.forEach(item => {
          localStorage.removeItem(item)
        })
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      } catch (e) {
        reject(e)
      }
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

